package com.monitor.entity.response;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 通用响应.
 *
 * @author Li Zhiyuan
 * @since 2020-11-12
 */
@Data
@Api("通用响应")
public class Rs<T> implements Serializable {
    @ApiModelProperty(value = "描述", name = "message")
    private String message;
    @ApiModelProperty(value = "请求结果code码", name = "code")
    private int code;
    @ApiModelProperty(value = "返回内容数据", name = "data")
    private T data;

    public static <R> Rs<R> of() {
        Rs<R> rs = new Rs<>();
        rs.setCode(ResultCode.SUCCESS.getCode());
        rs.setMessage(ResultCode.SUCCESS.getMsg());
        return rs;
    }

    public static <R> Rs<R> of(R data) {
        Rs<R> rs = new Rs<>();
        rs.setCode(ResultCode.SUCCESS.getCode());
        rs.setMessage(ResultCode.SUCCESS.getMsg());
        rs.setData(data);
        return rs;
    }

    public static <R> Rs<R> of(R data, HttpStatus httpStatus) {
        Rs<R> rs = new Rs<>();
        rs.setCode(httpStatus.value());
        rs.setMessage(httpStatus.getReasonPhrase());
        rs.setData(data);
        return rs;
    }

    public static <R> Rs<R> of(ResultCode resultCode) {
        Rs<R> rs = new Rs<>();
        rs.setCode(resultCode.getCode());
        rs.setMessage(resultCode.getMsg());
        return rs;
    }

    public static <R> Rs<R> of(R data, ResultCode resultCode) {
        Rs<R> rs = new Rs<>();
        rs.setCode(resultCode.getCode());
        rs.setMessage(resultCode.getMsg());
        rs.setData(data);
        return rs;
    }

    public static <R> Rs<R> fa() {
        Rs<R> rs = new Rs<>();
        rs.setCode(ResultCode.error.getCode());
        rs.setMessage(ResultCode.error.getMsg());
        return rs;
    }

    public static <R> Rs<R> fa(ResultCode resultCode) {
        Rs<R> rs = new Rs<>();
        rs.setCode(resultCode.getCode());
        rs.setMessage(resultCode.getMsg());
        return rs;
    }

    public static <R> Rs<R> fa(R data) {
        Rs<R> rs = new Rs<>();
        rs.setData(data);
        return rs;
    }

    public static <R> ResponseEntity<Rs<R>> ok() {
        return ResponseEntity.ok(Rs.of());
    }

    public static <R> ResponseEntity<Rs<R>> ok(R data) {
        return ResponseEntity.ok(Rs.of(data));
    }

    public static <R> ResponseEntity<Rs<R>> ok(R data, HttpStatus httpStatus) {
        return ResponseEntity.ok(Rs.of(data, httpStatus));
    }

    public static <R> ResponseEntity<Rs<R>> ok(R data, ResultCode resultCode) {
        return ResponseEntity.ok(Rs.of(data, resultCode));
    }

    public static <R> ResponseEntity<Rs<R>> fail() {
        return ResponseEntity.ok(Rs.fa());
    }

    public static <R> ResponseEntity<Rs<R>> fail(ResultCode resultCode) {
        return ResponseEntity.ok(Rs.fa(resultCode));
    }

    public static <R> ResponseEntity<Rs<R>> fail(R data) {
        return ResponseEntity.ok(Rs.fa(data));
    }

    public static class JsonMap extends HashMap<String, Object> implements Serializable {
        public JsonMap() {
        }

        public Rs.JsonMap put(String key, Object val) {
            super.put(key, val);
            return this;
        }
    }

    public static Rs.JsonMap map() {
        return new Rs.JsonMap();
    }
}
